package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: lx
 * @Time: 11:46
 * @Description:
 *   索引管理
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//后来封装
    @Autowired
    private ListService listService;


    //开始搜索 json串保存在请求体中  GET  URL
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);
    }


    //@Autowired
    //private ElasticsearchTemplate elasticsearchTemplate;//早期封装

    //mappings 映射
    @GetMapping("/mappings")
    public Result mappingsES(){
        elasticsearchRestTemplate.createIndex(Goods.class);// 创建库
        elasticsearchRestTemplate.putMapping(Goods.class);//  映射  表 字段  指定字段类型
        return Result.ok();
    }
    //上架    商品 sku  到ES索引库
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架   从ES库存中删除索引
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);

        return Result.ok();
    }
    //更新热度
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){

        //更新热度
        listService.hotScore(skuId);

        return Result.ok();
    }
}
